package com.weakteam.orderonline.dataresource.dao;

import com.weakteam.orderonline.dataresource.domain.Goods;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * @author Nail
 */

@Repository
public class GoodsDAOImpl implements GoodsDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<Goods> getAll() {
        return em.createQuery("FROM Goods g ORDER BY g.order").getResultList();
    }

    @Override
    public List<Goods> getAllEnabled() {
        final String query = "FROM Goods g WHERE g.enabled = true ORDER BY g.order";
        return em.createQuery(query).getResultList();
    }

    @Override
    public List<Goods> getAllByMenuId(Integer menuId) {
        final String query = "FROM Goods g WHERE g.menuId=:menuId ORDER BY g.order";
        return em.createQuery(query).setParameter("menuId", menuId).getResultList();
    }

    @Override
    public List<Goods> getAllEnabledByMenuId(Integer menuId) {
        final String query = "FROM Goods g WHERE g.menuId=:menuId and g.enabled = true ORDER BY g.order";
        return em.createQuery(query).setParameter("menuId", menuId).getResultList();
    }

    @Override
    public Goods getById(Integer id) {
        return em.find(Goods.class, id);
    }

    @Override
    public void save(Goods goods) {
        this.em.merge(goods);
    }

    @Override
    public boolean delete(Integer id) {
        final String query = "DELETE Goods g WHERE g.id = :id";
        int count = em.createQuery(query).setParameter("id", id).executeUpdate();
        return (count > 0);
    }

    @Override
    public Integer getMaxOrder(Integer menuId) {
        final String query = "SELECT max(g.order) AS max FROM Goods g WHERE g.menuId=:menuId";
        Integer maxOrder = (Integer)em.createQuery(query).setParameter("menuId", menuId).getSingleResult();
        if (maxOrder==null) maxOrder = 0;
        return maxOrder;
    }

    @Override
    public List<String> getImageFileList() {
        final String query = "SELECT distinct(g.image) AS img FROM Goods g WHERE g.image IS NOT null and g.image != 'null'";
        List<String> result = (List<String>)em.createQuery(query).getResultList();
        return result;
    }

    @Override
    public List<String> getIcoFileList() {
        final String query = "SELECT distinct(g.ico) as img FROM Goods g WHERE g.ico IS NOT null and g.ico != 'null'";
        List<String> result = (List<String>)em.createQuery(query).getResultList();
        return result;
    }

    @Override
    public List<String> getEnabledImageFileList() {
        final String query = "SELECT distinct(g.image) AS img FROM Goods g WHERE g.enabled = true and g.image IS NOT null and g.image != 'null'";
        List<String> result = (List<String>)em.createQuery(query).getResultList();
        return result;
    }

    @Override
    public List<String> getEnabledIcoFileList() {
        final String query = "SELECT distinct(g.ico) as img FROM Goods g WHERE g.enabled = true and g.ico IS NOT null and g.ico != 'null'";
        List<String> result = (List<String>)em.createQuery(query).getResultList();
        return result;
    }


}
